#pragma once
#include<iostream>
#include<vector>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<functional>
#include"InetAddr.hpp"
#include"Threadpool.hpp"
#include"LockGuard.hpp"
//类型别名
using task_t = std::function<void()>;
class MessageRoue
{
    private:
    bool IsExists(const InetAddr &addr)
    {
        for(auto a : _online_user)
        {
            if(a==addr)
            {
                return true;
            }
        }
        return false;
    }
    public:
    MessageRoue()
    {
        //互斥锁
        pthread_mutex_init(&_mutex,nullptr);
    }
    void Adduser(const InetAddr &addr)
    {
        //添加用户
        LockGuard lockguard(&_mutex);
        if(IsExists(addr))
        {
            return;
        }
        _online_user.push_back(addr);
    }
    void DelUser(const InetAddr &addr)
    {
        //删除用户
        LockGuard lockgyard(&_mutex);
        for(auto iter = _online_user.begin();iter!=_online_user.end();iter++)
        {
            if(*iter ==addr)
                {
                    _online_user.erase(iter);
                    break;
                }
        }
    }
    void RouteHelper(int sockfd,std::string message,InetAddr who)
    {
        LockGuard lockguard(&_mutex);
        //转发消息
        for(auto u:_online_user)
        {
            std::string send_message = "\n["+who.Ip()+":"+std::to_string(who.Port())+"]#"
            +message+"\n";
            struct sockaddr_in clientaddr = u.Addr();
            ::sendto(sockfd,send_message.c_str(),send_message.size(),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr));

        }
    }
    void Route(int sockfd,std::string message,InetAddr who)
    {
        //用户首次发消息，不只是发消息，还要将自己插入到在线用户中
        Adduser(who);
        //1.客户退出
        if(message=="Q"||message=="QUIT")
        {
            DelUser(who);//删除
        }
        //2:构建任务对象，入队列，让线程池进行转发
        task_t t = std::bind(&MessageRoue::RouteHelper,this,sockfd,message,who);
        ThreadPool<task_t>::gerInstance()->Enqueue(t);
    }
    ~MessageRoue()
    {
        pthread_mutex_destroy(&_mutex);
    }
    private:
    std::vector<InetAddr> _online_user;
    pthread_mutex_t _mutex;
};
